package cn.edu.xmut.chen.zhlg.zhlg.vacancy.controller;

import cn.edu.xmut.chen.zhlg.common.HttpResult.Result;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.entity.Collect;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.entity.Vacancy;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.param.CollectParam;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.result.VacancyPageResult;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.service.CollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    @PostMapping("")
    public boolean collect(@RequestBody Collect collect) {
        collect.setCreateTime(new java.util.Date());
        return collectService.save(collect);
    }

    @GetMapping("/list")
    public Result list(CollectParam param) {
        return Result.success("查询成功").data(collectService.getCollectedVacanciesByUserId(param));
    }

    @PostMapping("/deleteCollect")
    public boolean delete(@RequestBody String id) {
        return collectService.removeById(id);
    }

    @GetMapping("/check")
     public Result check(@RequestParam String userId, @RequestParam String jobId) {
        return Result.success("查询成功").data(collectService.lambdaQuery().eq(Collect::getUserId, userId).eq(Collect::getJobId, jobId).count() > 0);
    }

    @PostMapping("/changeCollected")
    public Result changeCollected(@RequestBody Map<String, String> map) {
        LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collect::getUserId, map.get("userId")).eq( Collect::getJobId, map.get("jobId"));
        boolean flag = false;
        if (collectService.count(wrapper) > 0) {
            flag = collectService.remove(wrapper);
        }
        return Result.success("操作成功").data(flag);
    }
} 